package com.qf.lanzhou.blog.api.controller.PageBreak;

import com.qf.lanzhou.blog.api.controller.BaseServletPost;
import com.qf.lanzhou.blog.api.pojo.entity.Article;
import com.qf.lanzhou.blog.api.pojo.entity.Auxiliary;
import com.qf.lanzhou.blog.api.pojo.entity.Page;
import com.qf.lanzhou.blog.api.service.ArticlePaginationService;
import com.qf.lanzhou.blog.api.service.impl.ArticlePaginationServicelmpl;
import com.qf.lanzhou.blog.api.service.impl.ArticleServiceImp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author jack
 * 文章分页服务器 所有功能 五敌版
 */
@WebServlet("/ArticlePagination1.do")
public class ArticlePaginationServlet extends BaseServletPost {
    public static int numPage = 1;
    public static int c = 0;
    private ArticlePaginationService aps = new ArticlePaginationServicelmpl();
    private Page page = new Page(1,20);
    private Page page1 = new Page(1,20);
    private ArticleServiceImp articleServiceImp = new ArticleServiceImp();
    private static String instr;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("默认分页服务器连接成功，dopost");
        //获取总页数

        Integer totalCounts = aps.getTotalCounts();
        page.setTotalCounts(totalCounts);
        //接收参数
        Auxiliary auxiliary = paramsToEntity(req, Auxiliary.class);
        //先判断c是否为空
        if (auxiliary.getC() != null) {
            Integer decode = Integer.decode(auxiliary.getC());
            c = decode;
        }
        System.out.println("c: "+c);
        Integer a = Integer.decode(auxiliary.getA());
        Integer b = Integer.decode(auxiliary.getB());
        //两级分割！
        if (c == 0) {
            System.out.println("b: "+b);
            if (b != -168) {
                if (b == 10086) {
                    numPage = page.getTotalPage();
                    List list = aps.Pagination(numPage);
                    successResultWrite(list, resp);
                    return;
                }
                //按照页码查询
                if (b <= 0 || b > page.getTotalPage()) {
                    String str = "超出或小于页码";
                    failedResultWrite(str, resp);
                    return;
                }
                numPage = b;
                List list = aps.Pagination(numPage);
                successResultWrite(list, resp);
                return;
            }
            if (a == 0) {
                //前进
                if (numPage == page.getTotalPage()) {
                    String str = "已经是最后一页了哦";
                    failedResultWrite(str, resp);
                    return;
                }
                numPage++;
                List list = aps.Pagination(numPage);
                successResultWrite(list, resp);
                return;
            } else {
                //后退
                if (numPage == 1) {
                    String str = "前面没有了哦";
                    failedResultWrite(str, resp);
                    return;
                }
                numPage--;
                List list = aps.Pagination(numPage);
                successResultWrite(list, resp);
                return;
            }
        }
        //---------------------------------------------------------------------------------------------

        if (c == 1) {
            //点赞排序
            System.out.println("进入点赞排序");
            if (b != -168) {
                //按照页码查询
                System.out.println("页码跳转");
                if (b == 10086) {
                    numPage = page.getTotalPage();
                    List list = aps.Pagination(numPage);
                    successResultWrite(list, resp);
                    return;
                }
                if (b <= 0 || b > page.getTotalPage()) {
                    String str = "超出或小于页码";
                    failedResultWrite(str, resp);
                    return;
                }
                numPage = b;
                List list = aps.kudosPagination(numPage);
                successResultWrite(list, resp);
                return;
            }
            if (a == 0) {
                //前进
                System.out.println("前进方法");
                System.out.println(numPage + "  " + page.getTotalPage());
                if (numPage == page.getTotalPage()) {
                    String str = "已经是最后一页了哦";
                    failedResultWrite(str, resp);
                    return;
                }
                numPage++;
                System.out.println(numPage);
                List list = aps.kudosPagination(numPage);
                successResultWrite(list, resp);
                return;
            } else {
                //后退1
                if (numPage == 1) {
                    String str = "前面没有了哦";
                    failedResultWrite(str, resp);
                    return;
                }
                numPage--;
                List list = aps.kudosPagination(numPage);
                successResultWrite(list, resp);
                return;
            }
        }

        //---------------------------------------------------------------------------------------------
        System.out.println("----------------------------");
        if (c == 2) {
            //国源的查询分页
            //获取总行数
            if (auxiliary.getSearchArticle() != null) {
                instr = auxiliary.getSearchArticle();
            }
            System.out.println(instr);
            Integer count = articleServiceImp.getCount(instr);
            System.out.println(count);
            page1.setTotalCounts(count);
            System.out.println(page1.getTotalPage());
            //如果总行数为0,则无搜索结果
            if (count == 0) {
                String msg = "没有查询内容";
                failedResultWrite(msg, resp);
                return;
            }
            //把查询语句拿出来赋值
            System.out.println("查询分页");
            if (b != -168) {
                //按照页码查询
                System.out.println("页码跳转");
                if (b==10086){
                    numPage = page1.getTotalPage();
                    List list = articleServiceImp.checkArticlePage(instr,numPage);
                    successResultWrite(list,resp);
                    return;
                }
                if (b <= 0 || b > page1.getTotalPage()) {
                    String str = "超出或小于页码";
                    failedResultWrite(str, resp);
                    return;
                }
                numPage = b;
                List list = articleServiceImp.checkArticlePage(instr, numPage);
                successResultWrite(list, resp);
                return;
            }
            if (a == 0) {
                //前进
                System.out.println("前进方法");
                if (numPage == page1.getTotalPage()) {
                    String str = "已经是最后一页了哦";
                    failedResultWrite(str, resp);
                    return;
                }
                numPage++;
                System.out.println(numPage);
                List list = articleServiceImp.checkArticlePage(instr, numPage);
                successResultWrite(list, resp);
                return;
            } else {
                //后退1
                if (numPage == 1) {
                    String str = "前面没有了哦";
                    failedResultWrite(str, resp);
                    return;
                }
                numPage--;
                List list = articleServiceImp.checkArticlePage(instr, numPage);
                successResultWrite(list, resp);
            }
        }
    }
}
